﻿using System.Collections.Specialized;
using JetBrains.Annotations;
using StreetPacMan.Server;
using StreetPacMan.Server.Facade;

namespace StreetPacMan.FrontEnd
{
    public interface IServerPollParametersConverter
    {
        ServerPollParameters GetServerPollParametersFromRequest(NameValueCollection nameValueCollection);
        GetGameStateParameters GetGameStateParametersFromRequest(NameValueCollection nameValueCollection);
        CreateGameParameters GetCreateGameParametersFromRequest(NameValueCollection nameValueCollection);
        JoinGameParameters GetJoinGameParametersFromRequest(NameValueCollection nameValueCollection);
    }

    [UsedByIoc]
    public class ServerPollParametersConverter : IServerPollParametersConverter
    {
        public ServerPollParameters GetServerPollParametersFromRequest(NameValueCollection nameValueCollection)
        {
            var entityId = int.Parse(nameValueCollection["EntityId"]);
            var geoLat = double.Parse(nameValueCollection["GeoLat"]);
            var geoLon = double.Parse(nameValueCollection["GeoLng"]);
            var isEnabled = bool.Parse(nameValueCollection["IsEnabled"]);

            var updateMeForTheLastXSeconds = int.Parse(nameValueCollection["UpdateMeForTheLastXSeconds"]);
            return new ServerPollParameters(entityId, geoLat, geoLon, isEnabled)
                       {UpdateMeForTheLastXSeconds = updateMeForTheLastXSeconds};
        }

        public GetGameStateParameters GetGameStateParametersFromRequest(NameValueCollection nameValueCollection)
        {
            var entityId = int.Parse(nameValueCollection["EntityId"]);
            return new GetGameStateParameters(entityId);
        }

        public CreateGameParameters GetCreateGameParametersFromRequest(NameValueCollection nameValueCollection)
        {
            var name = nameValueCollection["Name"];
            return new CreateGameParameters(name);
        }

        public JoinGameParameters GetJoinGameParametersFromRequest(NameValueCollection nameValueCollection)
        {
            var gameName = nameValueCollection["GameName"];
            var playerName = nameValueCollection["PlayerName"];
            var role = int.Parse(nameValueCollection["Role"]);
            return new JoinGameParameters(gameName, playerName, (PlayerRole)role);
        }
    }
}